home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C# & Game Programming - A…er's Guide (2nd Edition)
/
Buono 2nd Ed.iso
/
GameClasses
/
TimedEvent.cs
< prev
next >
Wrap
Text File
|
2004-09-07
|
2KB
|
69 lines
// TimedEvent.cs: A simple class representing a ticking timer
// that can be used in a number of ways. It can be used to
// simply count something that you want to have happen a set
// number of times. Derriving from this class and overriding
// the Tick() method can allow for custom behavior.
using System;
namespace GameClasses {
public delegate void TickHandler(TimedEvent e, Object payload);
public class TimedEvent {
// Member fields
public int ticks;
public int tickCounter;
public bool isCountdown;
public bool isActive;
public TickHandler remoteHandler;
public object payload;
public TimedEvent() {
ticks = 0;
isCountdown = false;
SetTickHandler(new TickHandler(DoNothing));
}
public TimedEvent(TickHandler handler) {
SetTickHandler(handler);
}
public TimedEvent(int numTicks, TickHandler handler) {
SetCountdown(numTicks);
SetTickHandler(handler);
}
public void SetTickHandler(TickHandler handler) {
remoteHandler = handler;
}
public void SetCountdown(int numTicks) {
isCountdown = numTicks > 0;
ticks = numTicks;
tickCounter = ticks;
}
public void Tick() {
if (!isActive)
return;
// Call any custom function
remoteHandler(this, payload);
// decrement the counter
if (isCountdown)
if (tickCounter > 0)
tickCounter--;
else
isActive = false;
}
public void Reset() {
tickCounter = ticks;
isActive = true;
}
// Dummy for default tick-handler
public void DoNothing(TimedEvent e, Object o) {}
}
}